package com.errorfinding.tabbitmqDemo.controller;

import com.errorfinding.tabbitmqDemo.config.ConfirmConfig;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 生产者
 * 开发发消息 测试 发布确认
 */
@RestController
@RequestMapping("/confirm")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    //发消息
    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable String message){

        CorrelationData correlationData = new CorrelationData("0");
        rabbitTemplate.convertAndSend(ConfirmConfig.confirm_exchange_name,ConfirmConfig.confirm_routing_key,message,correlationData);

        //测试1.可以将 交换机名称 换一下 测试 发布确认失败 交换机接收成功 队列接收成功  ConfirmConfig.confirm_exchange_name+"123"
        //测试2.可以将 routingkey 换一下 无法找到队列 测试 发布确认成功 交换机接收成功 队列接收不到消息   ConfirmConfig.confirm_routing_key+"123"
        CorrelationData correlationData1 = new CorrelationData("1");
        rabbitTemplate.convertAndSend(ConfirmConfig.confirm_exchange_name+"123",ConfirmConfig.confirm_routing_key,message,correlationData1);
        CorrelationData correlationData2 = new CorrelationData("2");
        rabbitTemplate.convertAndSend(ConfirmConfig.confirm_exchange_name,ConfirmConfig.confirm_routing_key+"1123",message,correlationData2);

        System.out.println("发送消息内容："+message);
    }
}
